﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PMS.IServices;
using PMS.Models.Base;
using PMS.Services;
using PMS.Utils;

namespace PMS.EasyUI.Areas.Base.Controllers
{
    public class BaseRoleController : Controller
    {
        IBaseRoleService _baseRoleService = new BaseRoleService();
        // GET: Base/BaseRole
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult GetList()
        {
            Hashtable ht = new Hashtable();
            string roleName = Request.Form["roleName"] ?? "";
            ht.Add("RoleName", roleName.ToDapperLike());

            int counts = 0;
            int pageIndex = Request.Form["page"].ToInt32();
            int pageSize = Request.Form["rows"].ToInt32();
            var data = _baseRoleService.GetList(ht, "Id", "Desc", pageIndex, pageSize, out counts);
            return Json(new { total = counts, rows = data }, "text/html", JsonRequestBehavior.AllowGet);
        }


        public ActionResult Add()
        {
            return View(new BaseRole());
        }

        [HttpPost]
        public ActionResult AddOrEdit(BaseRole role)
        {
            if (role.Id > 0)
            {
                ModelExtend<BaseRole>.SetModifyCommAll(role);
                if (_baseRoleService.Edit(role) > 0)
                {
                    return Json(Result.Success, "text/html", JsonRequestBehavior.AllowGet);
                }

                return Json(Result.Error, "text/html", JsonRequestBehavior.AllowGet);
            }
            else
            {
                ModelExtend<BaseRole>.SetAddCommAll(role);
                if (_baseRoleService.Add(role) > 0)
                {
                    return Json(Result.Success, "text/html", JsonRequestBehavior.AllowGet);
                }

                return Json(Result.Error, "text/html", JsonRequestBehavior.AllowGet);
            }
        }


        public ActionResult Edit(int Id)
        {
            BaseRole role = _baseRoleService.Dtl(Id);
            return View(role);
        }

    }
}